home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / test / ICMP / icmp_echo.c next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  5.4 KB  |  167 lines

  1. /*
  2.  *  $Id: icmp_echo.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  icmp_echo.c - ICMP_ECHO Packet assembly tester
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  */
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../../include/config.h"
  36. #endif
  37. #include "../libnet_test.h"
  38.  
  39. int
  40. main(int argc, char **argv)
  41. {
  42.     int sock, n, c, p_num;
  43.     struct libnet_arena arena, *arena_p;
  44.     u_char *packets[10];
  45.     u_long src_ip, dst_ip;
  46.     
  47.     printf("ICMP_ECHO / Arena allocator test\n");
  48.  
  49.     src_ip = 0;
  50.     dst_ip = 0;
  51.     while((c = getopt(argc, argv, "d:s:")) != EOF)
  52.     {
  53.         switch (c)
  54.         {
  55.             case 'd':
  56.                 if (!(dst_ip = libnet_name_resolve(optarg, 1)))
  57.                 {
  58.                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
  59.                     exit(1);
  60.                 }
  61.                 break;
  62.             case 's':
  63.                 if (!(src_ip = libnet_name_resolve(optarg, 1)))
  64.                 {
  65.                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
  66.                     exit(1);
  67.                 }
  68.                 break;
  69.         }
  70.     }
  71.     if (!src_ip || !dst_ip)
  72.     {
  73.         usage(argv[0]);
  74.         exit(EXIT_FAILURE);
  75.     }
  76.  
  77.     arena_p = &arena;
  78.     p_num = 10;
  79.     if (libnet_init_packet_arena(&arena_p, p_num, ICMP_ECHO_H + IP_H) == -1)
  80.     {
  81.         fprintf(stderr, "libnet_init_packet_arena failed\n");
  82.         exit(EXIT_FAILURE);
  83.     }
  84.     else
  85.     {
  86.         printf("Allocated an arena of %ld bytes..\n",
  87.             LIBNET_GET_ARENA_SIZE(arena));
  88.     }
  89.  
  90.     /*
  91.      *  Open our raw IP socket and set IP_HDRINCL.
  92.      */
  93.     sock = libnet_open_raw_sock(IPPROTO_RAW);
  94.     if (sock == -1)
  95.     {
  96.         perror("No socket");
  97.         exit(EXIT_FAILURE);
  98.     }
  99.     
  100.     for (n = 0; n < p_num; n++)
  101.     {
  102.         printf("%ld bytes remaining in arena\n",
  103.             LIBNET_GET_ARENA_REMAINING_BYTES(arena));
  104.         packets[n] = libnet_next_packet_from_arena(&arena_p,
  105.             ICMP_ECHO_H + IP_H);
  106.         if (!packets[n])
  107.         {
  108.             fprintf(stderr, "Arena is empty\n");
  109.             continue;
  110.         }
  111.  
  112.         /*
  113.          *  Build the IP header (shown exploded for commenting).
  114.          */
  115.         libnet_build_ip(ICMP_ECHO_H,              /* Size of the payload */
  116.                 IPTOS_LOWDELAY | IPTOS_THROUGHPUT, /* IP tos */
  117.                 242,                            /* IP ID */
  118.                 0,                              /* Frag stuff */
  119.                 48,                             /* TTL */
  120.                 IPPROTO_ICMP,                   /* Transport protocol */
  121.                 src_ip,                         /* Source IP */
  122.                 dst_ip,                         /* Destination IP */
  123.                 NULL,                           /* Pointer to payload (none) */
  124.                 0,
  125.                 packets[n]);                    /* Packet header memory */
  126.  
  127.         /*
  128.          *  Build the ICMP header.
  129.          */
  130.         libnet_build_icmp_echo(ICMP_ECHO,       /* type */
  131.                 0,                              /* code */
  132.                 242,                            /* id */
  133.                 1,                              /* seq */
  134.                 NULL,                            /* pointer to payload */
  135.                 0,                              /* size of payload */
  136.                 packets[n] + IP_H);             /* packet header memory */
  137.  
  138.         if (libnet_do_checksum(packets[n], IPPROTO_ICMP, ICMP_ECHO_H) == -1)
  139.         {
  140.             fprintf(stderr, "Can't do checksum!\n");
  141.         }
  142.  
  143.         /*
  144.          *  Write the packet to the network.
  145.          */
  146.         c = libnet_write_ip(sock, packets[n], ICMP_ECHO_H + IP_H);
  147.         if (c < ICMP_ECHO_H + IP_H)
  148.         {
  149.             fprintf(stderr, "write_ip\n");
  150.         }
  151.         printf("Completed %d of %d, wrote %d bytes\n", n + 1, p_num, c);
  152.     }
  153.  
  154.     libnet_destroy_packet_arena(&arena_p);
  155.     /* Blah. */
  156.     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
  157. }
  158.  
  159.  
  160. void
  161. usage(u_char *name)
  162. {
  163.     fprintf(stderr, "usage: %s -s source_ip -d destination_ip\n ", name);
  164. }
  165.  
  166. /* EOF */
  167.